import xlrd


class ReadExcel():
    """读取excel表格的类"""
    def __init__(self, file_name):
        self.file_name = file_name

    def __read_sheet(self, sheet_inx):
        """获取sheet"""
        data = xlrd.open_workbook(self.file_name)
        sheet = data.sheet_by_index(sheet_inx)
        return sheet

    def read_row(self, sheet_inx, row_num):
        """读取一行的数据"""
        row = self.__read_sheet(sheet_inx).row_values(row_num)
        return row

    def read_rows(self, sheet_inx):
        """读取所有行的数据"""
        row_num = self.__read_sheet(sheet_inx).nrows
        col_num = self.__read_sheet(sheet_inx).ncols
        lis = []
        for i in range(row_num):
            row_list = []
            for j in range(col_num):
                row_list.append(self.__read_sheet(sheet_inx).cell_value(i, j))
            lis.append(row_list)
        return lis


    def read_col(self, sheet_inx, col_num):

        col = self.__read_sheet(sheet_inx).col_values(col_num)
        return col

    def read_cell(self, sheet_inx, rown, coln):

        cell_val = self.__read_sheet(sheet_inx).cell_value(rown, coln)
        return cell_val


if __name__ == '__main__':
    read_exe = ReadExcel('../data/test01.xlsx')
    print(read_exe.read_cell(1,1,1))
    print(read_exe.read_rows(1))
